//
//  JSModelEditView.m
//  JigSaw
//
//  Created by Slim Wang on 15/1/29.
//  Copyright (c) 2015年 LITB. All rights reserved.
//

#import "JSModelEditView.h"
#import "VSUtils.h"
#import "JSImageEditPopupView.h"
@interface JSModelEditView ()

@property(nonatomic, assign)float minimumScale;

@end

@implementation JSModelEditView

- (void)initImageView
{
    self.backgroundColor = [UIColor clearColor];
    
    [_contentView removeFromSuperview];
    _contentView = [[UIScrollView alloc] initWithFrame:CGRectInset(self.bounds, 0, 0)];
    _contentView.delegate = self;
    _contentView.showsHorizontalScrollIndicator = NO;
    _contentView.showsVerticalScrollIndicator = NO;
    [self addSubview:_contentView];
    
    [_imageview removeFromSuperview];
    _imageview = [[UIImageView alloc] initWithFrame:self.bounds];
    _imageview.frame = CGRectMake(0, 0, [VSUtils screenWidth] * 2.5, [VSUtils screenWidth] * 2.5);
    _imageview.userInteractionEnabled = YES;
    [_contentView addSubview:_imageview];

    UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaped:)];
    [_imageview addGestureRecognizer:tapGesture];
    
    self.minimumScale = self.frame.size.width / _imageview.frame.size.width;
    [_contentView setMinimumZoomScale:self.minimumScale];
    [_contentView setZoomScale:self.minimumScale];
    
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    
    UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    [_imageview addGestureRecognizer:longPressGesture];
}

- (void)handleTaped:(UIGestureRecognizer *)gesture
{
    CGPoint point = [gesture locationInView:self.superview];
    if (self.tapBlk != NULL) {
        self.tapBlk(point, self);
    }
}

- (void)longPressed:(UIGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        [self contentShrink];
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        [self contentExpand];
        if (self.longPressedReleasedBlk != NULL) {
            self.longPressedReleasedBlk([gesture locationInView:self.superview], self);
        }
    }
    else if (gesture.state == UIGestureRecognizerStateChanged) {
        if (self.longPressedBlk != NULL) {
            self.longPressedBlk([gesture locationInView:self.superview], self);
        }
    }
}

- (void)setImageViewData:(UIImage *)imageData withMaskImage:(UIImage*)mask
{
    [self initImageView];
    _imageview.image= imageData;
    [_contentView setZoomScale:self.minimumScale];
    if (imageData == nil) {
        return;
    }
    
    CGRect rect  = CGRectZero;
    CGFloat scale = 1.0f;
    CGFloat w = 0.0f;
    CGFloat h = 0.0f;
    
    if(self.contentView.frame.size.width > self.contentView.frame.size.height)
    {
        
        w = self.contentView.frame.size.width;
        h = w*imageData.size.height/imageData.size.width;
        if(h < self.contentView.frame.size.height){
            h = self.contentView.frame.size.height;
            w = h*imageData.size.width/imageData.size.height;
        }
        
    }else{
        
        h = self.contentView.frame.size.height;
        w = h*imageData.size.width/imageData.size.height;
        if(w < self.contentView.frame.size.width){
            w = self.contentView.frame.size.width;
            h = w*imageData.size.height/imageData.size.width;
        }
    }
    rect.size = CGSizeMake(w, h);
    
    CGFloat scale_w = w / imageData.size.width;
    CGFloat scale_h = h / imageData.size.height;
    if (w > self.frame.size.width || h > self.frame.size.height) {
        scale_w = w / self.frame.size.width;
        scale_h = h / self.frame.size.height;
        if (scale_w > scale_h) {
            scale = 1/scale_w;
        }else{
            scale = 1/scale_h;
        }
    }
    
    if (w <= self.frame.size.width || h <= self.frame.size.height) {
        scale_w = w / self.frame.size.width;
        scale_h = h / self.frame.size.height;
        if (scale_w > scale_h) {
            scale = scale_h;
        }else{
            scale = scale_w;
        }
    }
    
    @synchronized(self){
        _imageview.frame = rect;
        if (mask) {
            self.maskImage = mask;
            CALayer* maskLayer = [CALayer layer];
            maskLayer.frame = self.bounds;
            maskLayer.contents = (__bridge id)([mask CGImage]);
            self.layer.mask = maskLayer;
        }
        
        [_contentView setZoomScale:0.2 animated:YES];
        
        [self setNeedsLayout];
        
    }
    
}
- (void)zoomIn
{
    float scale = _contentView.zoomScale;
    [_contentView setZoomScale:scale * 1.2 animated:YES];
}

- (void)zoomOut
{
    float scale = _contentView.zoomScale;
    [_contentView setZoomScale:scale * 0.8 animated:YES];
}

- (void)contentExpand
{
    [UIView animateWithDuration:0.3
                     animations:^{
                         _contentView.layer.affineTransform = CGAffineTransformMakeScale(1, 1);
                     }];
}

- (void)contentShrink
{
    [UIView animateWithDuration:0.3
                     animations:^{
                         _contentView.layer.affineTransform = CGAffineTransformMakeScale(0.9, 0.9);
                     }];
}


#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageview;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    
    [scrollView setZoomScale:scale animated:NO];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    return;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    return;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self.superview];
    self.imageview.center = touch;
    
}

@end
